package com.example.dao;

import com.example.bean.ProjectBean;
import com.example.bean.ReportBean;
import com.example.bean.UserBean;
import com.example.mapper.generator.ProjectPoMapper;
import com.example.mapper.generator.StudentPoMapper;
import com.example.mapper.generator.TeamProjectPoMapper;
import com.example.mapper.generator.TeamStudentPoMapper;
import com.example.mapper.generator.po.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Repository
public class StudentDao {
    @Autowired
    StudentPoMapper studentPoMapper;
    @Autowired
    TeamStudentPoMapper teamStudentPoMapper;
    @Autowired
    TeamProjectPoMapper teamProjectPoMapper;
    @Autowired
    ProjectPoMapper projectPoMapper;
    @Autowired
    TeamDao teamDao;
    @Autowired
    ProjectDao projectDao;

    public UserBean login(Long id, String password) {
        StudentPoExample example = new StudentPoExample();
        StudentPoExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andPasswordEqualTo(password);
        List<StudentPo> studentPoList = studentPoMapper.selectByExample(example);
        if (studentPoList.size() != 1) {
            return null;
        } else {
            StudentPo studentPo = studentPoList.get(0);
            return UserBean.builder().id(id).name(studentPo.getName()).password(studentPo.getPassword()).phone(studentPo.getPhone()).email(studentPo.getEmail()).identity("student").build();
        }
    }

    public Long register(String name, String password, String phone, String email) {
        StudentPo po = StudentPo.builder().name(name).password(password).phone(phone).email(email).build();
        studentPoMapper.insert(po);
        return po.getId();
    }



    public List<ProjectBean> getProjectsByStudentId(Long studentId) {
        List<Long> teamIds = teamDao.getTeamIdsByStudentId(studentId);
        List<ProjectBean> projects = new ArrayList<>();
        teamIds.forEach(teamId -> { // 根据队伍id查询项目
            List<Long> projectIds = projectDao.getProjectIdsByTeamId(teamId);
            projectIds.forEach(projectId -> {
                ProjectPo po = projectPoMapper.selectByPrimaryKey(projectId);
                ProjectBean project = ProjectBean.builder().id(po.getId()).name(po.getName()).start(po.getStart()).end(po.getEnd()).description(po.getDescription()).build();
                project.setStatus(LocalDate.now().isAfter(po.getStart()) && LocalDate.now().isBefore(po.getEnd()));
                projects.add(project);
            });
        });
        return projects;
    }

    public List<ReportBean> getReportsByStudentId(Long studentId) {
        List<Long> teamIds = teamDao.getTeamIdsByStudentId(studentId);
        List<ReportBean> reports = new ArrayList<>();
        teamIds.forEach(teamId -> { // 根据队伍id查询项目报告
            List<ReportBean> teamReports = projectDao.getReportsByTeamId(teamId);
            reports.addAll(teamReports);
        });
        return reports;
    }

    public UserBean getStudentById(Long studentId) {
        StudentPo po = this.studentPoMapper.selectByPrimaryKey(studentId);
        UserBean user = UserBean.builder().id(studentId).name(po.getName()).password(po.getPassword()).phone(po.getPhone()).email(po.getEmail()).identity("student").build();
        return user;
    }
}
